home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / games_d / rtanksrc.zip / GINPUT.C < prev    next >
C/C++ Source or Header  |  1995-03-07  |  3KB  |  148 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <alloc.h>
  4. #include <string.h>
  5. #include "dtypes.h"
  6. #include <conio.h>
  7. #include <ctype.h>
  8. #include "nkeybd.h"
  9. #include <graphics.h>
  10. #include "ginput.h"
  11.  
  12. static int x,y,l,maxlen;
  13. static char b[80];
  14.  
  15. /*
  16.  * Function : void _add_char(int ch)
  17.  * Purpose  : Append a character to buf
  18.  * Date     : 10/06/1988 22:40:56
  19.  */
  20. void _add_char(int ch)
  21. {
  22. char bf[5];
  23.  
  24.    if (l<maxlen) {
  25.       b[l++]=ch;
  26.       b[l]=NULL;
  27.       bf[1]=0;
  28.       bf[0]=ch;
  29.       moveto(x,y); outtext(bf);
  30.  
  31.       x+=textwidth("A");
  32.  
  33.    } else putchar(7);
  34.  
  35. } /* void _add_char(int ch) */
  36.  
  37. /*
  38.  * Function : void _del_char(void)
  39.  * Purpose  : Deletes the last character in the buffer
  40.  * Date     : 10/06/1988 22:48:38
  41.  */
  42. void _del_char(void)
  43. {
  44. char bf[5];
  45.  
  46.    if (l>0) {
  47.  
  48.       x-=textwidth("A");
  49.  
  50.       setcolor(CGA_WHITE);
  51.       bf[0]=b[l-1];
  52.       bf[1]=0;
  53.       moveto(x,y); outtext(bf);
  54.       setcolor(BLACK);
  55.  
  56.       b[--l]=NULL;
  57.  
  58.    } else putchar(7);
  59. } /* void _del_char(void) */
  60.  
  61. /*
  62.  * Function : char *ggetline(int len, char *s)
  63.  * Purpose  : Gets a LEN length string from keyboard and stores it in S.
  64.  * Date     : 10/06/1988 20:06:03
  65.  */
  66. char *ggetline(int len, char *s)
  67. {
  68. int ox,oy,a;
  69. BOOL first;
  70. char ch;
  71.  
  72.    x=ox=getx(); y=oy=gety();
  73.    l=0; maxlen=len;
  74.    first=TRUE;
  75.  
  76.    setcolor(BLACK);
  77.    moveto(x,y);
  78.    if (*s) outtext(s);
  79.    moveto(ox,oy);
  80.  
  81.    do {
  82.       moveto(x,y+8);
  83.       setcolor(BLACK);
  84.       linerel(8,0);
  85.       moveto(x,y);
  86.  
  87.       while (!kbhit())
  88.          ;
  89.       ch=getkey(); ch=toupper(ch);
  90.  
  91.       moveto(x,y+8);
  92.       setcolor(CGA_WHITE);
  93.       linerel(8,0);
  94.       setcolor(BLACK);
  95.       moveto(x,y);
  96.  
  97.       if (isalnum(ch)) {
  98.  
  99.          if (first) {
  100.             moveto(ox,oy);
  101.             setcolor(CGA_WHITE);
  102.             outtext(s);
  103.             setcolor(BLACK);
  104.             moveto(ox,oy);
  105.             first=FALSE;
  106.          }
  107.          _add_char(ch);
  108.  
  109.       } else {
  110.          switch(ch) {
  111.             case ESC: moveto(ox,oy); setcolor(CGA_WHITE);
  112.                       if (first)
  113.                           outtext(s);
  114.                       else outtext(b);
  115.                       strcpy(b," ");
  116.                       first=FALSE;
  117.                       ch=CR;
  118.                       /* No break statement here */
  119.  
  120.             case CR : if (first) {
  121.                          strcpy(b,s);
  122.                          x+=strlen(s);
  123.                       }
  124.  
  125.                       moveto(ox,oy);
  126.                       b[len]=NULL;
  127.                       break;
  128.  
  129.             case BS : _del_char();
  130.                       if (l==0) {
  131.                          moveto(ox,oy);
  132.                          if (*s) outtext(s);
  133.                          moveto(ox,oy);
  134.                          first=TRUE;
  135.                       }
  136.                       break;
  137.  
  138.             default : putchar(7); break;
  139.          }
  140.       } /* if not printable character */
  141.    } while (ch!=CR);
  142.  
  143.    strcpy(s,b);
  144.  
  145.    return(s);
  146. } /* char *ggetline(int len, char *s) */
  147.  
  148.